//
//  HomeViewController.m
//  halfSweat
//
//  Created by 甘伟杰 on 16/5/12.
//  Copyright © 2016年 ganweijie. All rights reserved.
//

#import "HomeViewController.h"
#import "HomePageItem.h"

#import <AFNetworking.h>
#import <MJExtension.h>

@interface HomeViewController ()

/** 模型数组 */
@property (strong, nonatomic) NSArray<HomePageItem *> *pageItems;

@end

@implementation HomeViewController

#pragma mark - 懒加载

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    UILabel *label = [[UILabel alloc] init];
    label.text = @"每日精选";
    [label sizeToFit];
    self.navigationItem.titleView = label;
    
    AFHTTPSessionManager *httpManager = [AFHTTPSessionManager manager];
//    NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
//    paraDict[@""] =
    NSString *get = @"http://www.duitang.com/napi/ad/banner/list/?locale=zh_CN&limit=0&screen_height=480&screen_width=320&ad_id=IGA009&start=0&platform_version=8.3&query_type=normal&device_platform=iPhone4%2C1&platform_name=iPhone%20OS&app_code=gandalf&__domain=www.duitang.com&app_version=6.0.7%20rv%3A158667&device_name=iPhone%204S";
    
    [httpManager GET:get parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject)
    {   // 数据解析
        NSArray *items = responseObject[@"data"][@"object_list"];
        self.pageItems = [HomePageItem mj_objectArrayWithKeyValuesArray:items];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark - 表视图代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pageItems.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    
    HomePageItem *item = self.pageItems[indexPath.row];
//    tableViewCell.imageView.image = [UIImage imageNamed:item.icon_url];
    tableViewCell.textLabel.text = item.desc;
    tableViewCell.detailTextLabel.text = item.stitle;
    
    return tableViewCell;
}

@end
